﻿using BCCommon;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using WeChatSDK.WeChatDto.CustomSerivce;
using WeChatSDK.WeChatDto.CustomSerivce.Message;
using WeChatSDK.WeChatServiceComponent.CustomService.KeyAccount;

namespace BCService.WeChatComponent.WeChatKeyAccountService
{
    /// <summary>
    /// 微信客服服务
    /// </summary>
    public class WeChatKeyAccountService : IWeChatKeyAccountService
    {
        private readonly KeyAccountService keyAccountService;
        public WeChatKeyAccountService()
        {
            keyAccountService = new KeyAccountService();
        }
        public string AddKeyAccount(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            CheckWeChatKeyAccountData(weChatKeyAccountDto);
            if (string.IsNullOrWhiteSpace(weChatKeyAccountDto.Nikename))
            {
                throw new ArgumentException("微信客服昵称不允许为空。");
            }
            Match match = Regex.Match(weChatKeyAccountDto.Nikename, RegexPatterns.CHINESE);
            if (match.Success && weChatKeyAccountDto.Nikename.Length > 16)
            {
                throw new ArgumentException("微信客服昵称最长16个汉字。");
            }
            TransformPasswordMD5(weChatKeyAccountDto);
            return WeChatHttpResponseHandle.ProcessMessage(keyAccountService.Add(weChatKeyAccountDto));
        }

        public string DeleteKeyAccount(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            CheckWeChatKeyAccountData(weChatKeyAccountDto);

            return WeChatHttpResponseHandle.ProcessMessage(keyAccountService.Delete(weChatKeyAccountDto));
        }

        public string UpdateKeyAccount(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            CheckWeChatKeyAccountData(weChatKeyAccountDto);
            if (string.IsNullOrWhiteSpace(weChatKeyAccountDto.Nikename))
            {
                throw new ArgumentException("微信客服昵称不允许为空。");
            }
            Match match = Regex.Match(weChatKeyAccountDto.Nikename, RegexPatterns.CHINESE);
            if (match.Success && weChatKeyAccountDto.Nikename.Length > 16)
            {
                throw new ArgumentException("微信客服昵称最长16个汉字。");
            }
            TransformPasswordMD5(weChatKeyAccountDto);
            return WeChatHttpResponseHandle.ProcessMessage(keyAccountService.Update(weChatKeyAccountDto));
        }

        public void UploadAvatar(string account, string avatarPath)
        {
            WeChatHttpResponseHandle.ProcessMessage(keyAccountService.Avatar(new WeChatKeyAccountDto() { KfAccount = account, KfHeadimgurl = avatarPath }));
        }

        public string SendBindingInvite(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            CheckWeChatKeyAccountData(weChatKeyAccountDto);
            if (string.IsNullOrWhiteSpace(weChatKeyAccountDto.InviteWx))
            {
                throw new ArgumentException("被邀请绑定的客服微信账号不能为空。");
            }
            return WeChatHttpResponseHandle.ProcessMessage(keyAccountService.SendBindingInvite(weChatKeyAccountDto));
        }

        public string Get()
        {
            return keyAccountService.Get().ResponseMessage;
        }
        
        public string SendMessage<T>(T weChatKeyAccountMessageDto) where T : WeChatKeyAccountMessageDto
        {
            return WeChatHttpResponseHandle.ProcessMessage(keyAccountService.SendMessage<T>(weChatKeyAccountMessageDto));
        }

        private void CheckWeChatKeyAccountData(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            if (string.IsNullOrWhiteSpace(weChatKeyAccountDto.KfAccount))
            {
                throw new ArgumentException("微信客服账号不允许为空。");
            }
            if (weChatKeyAccountDto.KfAccount.IndexOf("@") > 10)
            {
                throw new ArgumentException("微信客服账号前缀只允许最多10个字符。");
            }
            //if (string.IsNullOrWhiteSpace(weChatKeyAccountDto.KfId))
            //{
            //    throw new ArgumentException("微信客服工号不允许为空。");
            //}

        }

        private void TransformPasswordMD5(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            if (!string.IsNullOrWhiteSpace(weChatKeyAccountDto.Password))
            {
                MD5 mD5 = MD5.Create();
                byte[] bufferPassword = mD5.ComputeHash(Encoding.Default.GetBytes(weChatKeyAccountDto.Password));
                string newPassword = string.Empty;
                for (int i = 0; i < bufferPassword.Length; i++)
                {
                    newPassword += bufferPassword[i].ToString("x2");
                }
                weChatKeyAccountDto.Password = newPassword;
            }
        }

    }
}
